package com.webshop.DataBase;

import com.webshop.JavaBean.User;
import com.webshop.Utils.ServletOut;
import com.webshop.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/perconal")
public class PerCenServlet extends javax.servlet.http.HttpServlet {
     UserService userService=new UserService();
    @Override
    public void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        String method = req.getParameter("method");
        switch (method){
            case "get":
                get(req,resp);
                break;
            case "update":
                update(req,resp);
                break;
            default:
                break;
        }
    }


    public void get(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        String name =  String .valueOf(session.getAttribute("name"));
        User user=new User();
        try{
            user=userService.usermodify(name);
            String phone=user.getPhone();
            String email=user.getMail();
            String username=user.getName();
            System.out.println(phone+email);
            req.setAttribute("phone",phone);
            req.setAttribute("emil",email);
            req.setAttribute("name",username);
            ServletOut.request(req,resp,"/html/gerenzhongxin.jsp");
        }catch (Exception e){
               System.out.println("flase");
        }

    }
     public void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         HttpSession session = req.getSession();
         String name =  String .valueOf(session.getAttribute("name"));
         String phone=req.getParameter("phone");
         String email=req.getParameter("emil");
         System.out.println(name+phone+email);
         try {
             userService.updataper(phone,email,name);
             resp.sendRedirect("/html/gerenzhongxin.jsp");
         }catch (Exception e){
                 System.out.println("flase");
         }

    }
}
